/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.calendar.TZIDMapper;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.KeyValuePair;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AccessManager.AttrRightChecker;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.AttributeManager.IDNType;
/*     */ import com.zimbra.cs.account.CalendarResource;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.EntrySearchFilter;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Multi;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Operator;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Single;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Visitor;
/*     */ import com.zimbra.cs.account.IDNUtil;
/*     */ import com.zimbra.cs.account.Identity;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Signature;
/*     */ import com.zimbra.cs.account.Signature.SignatureContent;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.Right;
/*     */ import com.zimbra.cs.account.accesscontrol.ZimbraACE;
/*     */ import com.zimbra.soap.admin.type.DataSourceType;
/*     */ import java.util.Iterator;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ import java.util.Stack;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ToXML
/*     */ {
/*     */   static Element encodeAccount(Element parent, Account account)
/*     */   {
/*  56 */     return encodeAccount(parent, account, true, null, null);
/*     */   }
/*     */   
/*     */   static Element encodeAccount(Element parent, Account account, boolean applyCos, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */   {
/*  61 */     Element acctElem = parent.addNonUniqueElement("account");
/*  62 */     acctElem.addAttribute("name", account.getUnicodeName());
/*  63 */     acctElem.addAttribute("id", account.getId());
/*  64 */     Map attrs = account.getUnicodeAttrs(applyCos);
/*  65 */     encodeAttrs(acctElem, attrs, "n", reqAttrs, attrRightChecker);
/*  66 */     return acctElem;
/*     */   }
/*     */   
/*     */   static Element encodeCalendarResource(Element parent, CalendarResource resource) {
/*  70 */     return encodeCalendarResource(parent, resource, false, null, null);
/*     */   }
/*     */   
/*     */   static Element encodeCalendarResource(Element parent, CalendarResource resource, boolean applyCos) {
/*  74 */     return encodeCalendarResource(parent, resource, applyCos, null, null);
/*     */   }
/*     */   
/*     */   static Element encodeCalendarResource(Element parent, CalendarResource resource, boolean applyCos, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */   {
/*  79 */     Element resElem = parent.addNonUniqueElement("calresource");
/*  80 */     resElem.addAttribute("name", resource.getUnicodeName());
/*  81 */     resElem.addAttribute("id", resource.getId());
/*  82 */     Map attrs = resource.getUnicodeAttrs(applyCos);
/*  83 */     encodeAttrs(resElem, attrs, "n", reqAttrs, attrRightChecker);
/*  84 */     return resElem;
/*     */   }
/*     */   
/*     */   public static Element encodeCalendarResource(Element parent, String id, String name, Map attrs, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */   {
/*  89 */     Element resElem = parent.addNonUniqueElement("calresource");
/*  90 */     resElem.addAttribute("name", name);
/*  91 */     resElem.addAttribute("id", id);
/*  92 */     encodeAttrs(resElem, attrs, "n", reqAttrs, attrRightChecker);
/*  93 */     return resElem;
/*     */   }
/*     */   
/*     */   static void encodeAttrs(Element e, Map attrs, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker) {
/*  97 */     encodeAttrs(e, attrs, "n", reqAttrs, attrRightChecker);
/*     */   }
/*     */   
/*     */   private static void encodeAttrs(Element e, Map attrs, String key, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker) {
/* 101 */     AttributeManager attrMgr = null;
/*     */     try {
/* 103 */       attrMgr = AttributeManager.getInstance();
/*     */     } catch (ServiceException se) {
/* 105 */       ZimbraLog.account.warn("failed to get AttributeManager instance", se);
/*     */     }
/*     */     
/* 108 */     for (Iterator iter = attrs.entrySet().iterator(); iter.hasNext();) {
/* 109 */       Map.Entry entry = (Map.Entry)iter.next();
/* 110 */       String name = (String)entry.getKey();
/* 111 */       Object value = entry.getValue();
/*     */       
/*     */ 
/* 114 */       if (!name.equalsIgnoreCase("zimbraDataSourcePassword"))
/*     */       {
/*     */ 
/* 117 */         value = Provisioning.sanitizedAttrValue(name, value);
/*     */         
/*     */ 
/* 120 */         if ((reqAttrs == null) || (reqAttrs.contains(name)))
/*     */         {
/*     */ 
/* 123 */           boolean allowed = attrRightChecker == null ? true : attrRightChecker.allowAttr(name);
/*     */           
/* 125 */           AttributeManager.IDNType idnType = AttributeManager.idnType(attrMgr, name);
/*     */           
/* 127 */           if ((value instanceof String[])) {
/* 128 */             String[] sv = (String[])value;
/* 129 */             for (int i = 0; i < sv.length; i++) {
/* 130 */               encodeAttr(e, name, sv[i], "a", key, idnType, allowed);
/*     */             }
/* 132 */           } else if ((value instanceof String)) {
/* 133 */             value = fixupZimbraPrefTimeZoneId(name, (String)value);
/* 134 */             encodeAttr(e, name, (String)value, "a", key, idnType, allowed);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static class EntrySearchFilterXmlVisitor implements EntrySearchFilter.Visitor { Stack<Element> mParentStack;
/*     */     Element mRootElement;
/*     */     
/* 144 */     public EntrySearchFilterXmlVisitor(Element parent) { this.mParentStack = new Stack();
/* 145 */       this.mParentStack.push(parent);
/*     */     }
/*     */     
/* 148 */     public Element getRootElement() { return this.mRootElement; }
/*     */     
/*     */     public void visitSingle(EntrySearchFilter.Single term)
/*     */     {
/* 152 */       Element parent = (Element)this.mParentStack.peek();
/* 153 */       Element elem = parent.addNonUniqueElement("cond");
/* 154 */       if (this.mRootElement == null) this.mRootElement = elem;
/* 155 */       if (term.isNegation())
/* 156 */         elem.addAttribute("not", true);
/* 157 */       elem.addAttribute("attr", term.getLhs());
/* 158 */       elem.addAttribute("op", term.getOperator().toString());
/* 159 */       elem.addAttribute("value", term.getRhs());
/*     */     }
/*     */     
/*     */     public void enterMulti(EntrySearchFilter.Multi term)
/*     */     {
/* 164 */       Element parent = (Element)this.mParentStack.peek();
/* 165 */       Element elem = parent.addNonUniqueElement("conds");
/* 166 */       if (this.mRootElement == null) this.mRootElement = elem;
/* 167 */       if (term.isNegation())
/* 168 */         elem.addAttribute("not", true);
/* 169 */       if (!term.isAnd())
/* 170 */         elem.addAttribute("or", true);
/* 171 */       this.mParentStack.push(elem);
/*     */     }
/*     */     
/*     */     public void leaveMulti(EntrySearchFilter.Multi term)
/*     */     {
/* 176 */       this.mParentStack.pop();
/*     */     }
/*     */   }
/*     */   
/*     */   public static Element encodeEntrySearchFilter(Element parent, EntrySearchFilter filter) {
/* 181 */     EntrySearchFilterXmlVisitor visitor = new EntrySearchFilterXmlVisitor(parent);
/* 182 */     filter.traverse(visitor);
/* 183 */     return visitor.getRootElement();
/*     */   }
/*     */   
/*     */   public static Element encodeLocale(Element parent, Locale locale, Locale inLocale) {
/* 187 */     Element e = parent.addNonUniqueElement("locale");
/* 188 */     String id = locale.toString();
/*     */     
/*     */ 
/* 191 */     String name = L10nUtil.getMessage("L10nMsg", id, Locale.getDefault(), new Object[0]);
/* 192 */     if (name == null) {
/* 193 */       name = locale.getDisplayName(inLocale);
/*     */     }
/*     */     
/*     */ 
/* 197 */     String localName = locale.getDisplayName(inLocale);
/*     */     
/* 199 */     e.addAttribute("id", id);
/* 200 */     e.addAttribute("name", name != null ? name : id);
/* 201 */     e.addAttribute("localName", localName != null ? localName : id);
/* 202 */     return e;
/*     */   }
/*     */   
/*     */   public static Element encodeIdentity(Element parent, Identity identity) {
/* 206 */     Element e = parent.addNonUniqueElement("identity");
/* 207 */     e.addAttribute("name", identity.getName());
/* 208 */     e.addAttribute("id", identity.getId());
/* 209 */     encodeAttrs(e, identity.getUnicodeAttrs(), "name", null, null);
/* 210 */     return e;
/*     */   }
/*     */   
/*     */   public static Element encodeSignature(Element parent, Signature signature) {
/* 214 */     Element e = parent.addNonUniqueElement("signature");
/* 215 */     e.addAttribute("name", signature.getName());
/* 216 */     e.addAttribute("id", signature.getId());
/*     */     
/* 218 */     Set<Signature.SignatureContent> contents = signature.getContents();
/* 219 */     for (Signature.SignatureContent c : contents) {
/* 220 */       e.addNonUniqueElement("content").addAttribute("type", c.getMimeType()).addText(c.getContent());
/*     */     }
/*     */     
/*     */ 
/* 224 */     String contactId = signature.getAttr("zimbraPrefMailSignatureContactId");
/* 225 */     if (contactId != null) {
/* 226 */       e.addNonUniqueElement("cid").setText(contactId);
/*     */     }
/* 228 */     return e;
/*     */   }
/*     */   
/*     */   public static Element encodeDataSource(Element parent, DataSource ds) {
/* 232 */     Element e = parent.addNonUniqueElement("dataSource");
/* 233 */     e.addAttribute("name", ds.getName());
/* 234 */     e.addAttribute("id", ds.getId());
/* 235 */     e.addAttribute("type", ds.getType().name());
/* 236 */     encodeAttrs(e, ds.getUnicodeAttrs(), "n", null, null);
/* 237 */     return e;
/*     */   }
/*     */   
/*     */ 
/*     */   public static void encodeAttr(Element parent, String key, String value, String eltname, String attrname, AttributeManager.IDNType idnType, boolean allowed)
/*     */   {
/*     */     Element.KeyValuePair kvPair;
/* 244 */     if (allowed) {
/* 245 */       kvPair = parent.addKeyValuePair(key, IDNUtil.toUnicode(value, idnType), eltname, attrname);
/*     */     } else {
/* 247 */       Element.KeyValuePair kvPair = parent.addKeyValuePair(key, "", eltname, attrname);
/* 248 */       kvPair.addAttribute("pd", true);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void encodeAttr(Element response, String key, Object value) {
/* 253 */     if ((value instanceof String[])) {
/* 254 */       String[] sa = (String[])value;
/* 255 */       for (int i = 0; i < sa.length; i++) {
/* 256 */         if (!Strings.isNullOrEmpty(sa[i])) {
/* 257 */           response.addKeyValuePair(key, sa[i], "attr", "name");
/*     */         }
/*     */       }
/* 260 */     } else if (((value instanceof String)) && 
/* 261 */       (!Strings.isNullOrEmpty((String)value))) {
/* 262 */       response.addKeyValuePair(key, (String)value, "attr", "name");
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String fixupZimbraPrefTimeZoneId(String attrName, String attrValue)
/*     */   {
/* 271 */     if ("zimbraPrefTimeZoneId".equals(attrName)) {
/* 272 */       return TZIDMapper.canonicalize(attrValue);
/*     */     }
/* 274 */     return attrValue;
/*     */   }
/*     */   
/*     */   public static Element encodeACE(Element parent, ZimbraACE ace) {
/* 278 */     Element eACE = parent.addNonUniqueElement("ace").addAttribute("zid", ace.getGrantee()).addAttribute("gt", ace.getGranteeType().getCode()).addAttribute("right", ace.getRight().getName()).addAttribute("d", ace.getGranteeDisplayName());
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 284 */     if (ace.getGranteeType() == GranteeType.GT_KEY) {
/* 285 */       eACE.addAttribute("key", ace.getSecret());
/* 286 */     } else if (ace.getGranteeType() == GranteeType.GT_GUEST) {
/* 287 */       eACE.addAttribute("pw", ace.getSecret());
/*     */     }
/* 289 */     if (ace.deny()) {
/* 290 */       eACE.addAttribute("deny", ace.deny());
/*     */     }
/* 292 */     return eACE;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/ToXML.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */